package com.smart.persistence.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;



@Entity
@Table(name = "T_Room")
@NamedQueries({
    @NamedQuery(name = Room.findAvailableInPeriodByCodRoomType, 
    		query = "SELECT entity FROM Room entity left join entity.booksRoom as books where entity.roomType.codRoom = ?1 and (" +
    				"entity.booksRoom is empty or ( (books.checkIn <= ?2 and books.checkOut <= ?2) or (books.checkIn >= ?3 and books.checkOut >= ?3))) ")
      		
})
public class Room {
	
	public static final String findAvailableInPeriodByCodRoomType = "Room.findAvailableInPeriodByCodRoomType";
	
	
	@Id
	@Column(name = "id_room")
	@TableGenerator(name = "ROOM_GEN", allocationSize = 1, initialValue = 1, table = "T_ID", pkColumnName = "key", 
			valueColumnName = "valor", pkColumnValue = "ID_ROOM")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "ROOM_GEN")
	private long id;
	
	private int number;
	
	
	@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_room_type", nullable = false)    
	private RoomModel roomType;

	
	@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy = "room")	
	private List<BookRoom> booksRoom;


	public long getId() {
		return id;
	}


	public void setId(long id) {
		this.id = id;
	}


	public int getNumber() {
		return number;
	}


	public void setNumber(int number) {
		this.number = number;
	}


	public RoomModel getRoomType() {
		return roomType;
	}


	public void setRoomType(RoomModel roomType) {
		this.roomType = roomType;
	}


	public List<BookRoom> getBooksRoom() {
		return booksRoom;
	}


	public void setBooksRoom(List<BookRoom> booksRoom) {
		this.booksRoom = booksRoom;
	}


	@Override
	public String toString() {
		return "Room [id=" + id + ", number=" + number + ", roomType="
				+ roomType + ", booksRoom=]";
	}
	
	

}
